package com.company.dao.impl;

import com.company.dao.Fooddao;
import com.company.entity.Food;
import com.company.jdbc.jdbcutils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Fooddaoimpl implements Fooddao {
       @Override
       public List<Food> Findall() throws SQLException {
           Connection connection= jdbcutils.getConnection();
           Statement statement = connection.createStatement();
           String sql = "select * from food";
           ResultSet resultSet = statement.executeQuery(sql);
           List<Food> lis = new ArrayList<Food>();
           while (resultSet.next()){
               Food food = new Food();
               int id = resultSet.getInt("id");
               String name = resultSet.getString("food_name");
               double price  = resultSet.getDouble("price");
               String img = resultSet.getString("food_img");
               String type =resultSet.getString("food_type");
               int halal = resultSet.getInt("is_halal");
               food.setId(id);
               food.setFood_name(name);
               food.setPrice(price);
               food.setFood_img(img);
               food.setFood_type(type);
               food.setIs_halal(halal);

               lis.add(food);

           }


           return lis;
       }

    @Override
    public boolean saveFood(Food food) throws SQLException {
           boolean flag= false;
           Connection connection=jdbcutils.getConnection();
           String sql = "insert into food values(null,?,?,?,?,?)";
           PreparedStatement preparedStatement = connection.prepareStatement(sql);
           preparedStatement.setObject(1,food.getFood_name());
        preparedStatement.setObject(2,food.getFood_type());
        preparedStatement.setObject(3,food.getPrice());
        preparedStatement.setObject(4,food.getIs_halal());
        preparedStatement.setObject(5,food.getFood_img());
        int count  = preparedStatement.executeUpdate();
        if(count>0){
            flag = true;

        }
        return flag;
    }



    @Override
    public boolean upDateFood(Food food) throws SQLException {
           boolean flag = false;
            Connection connection=jdbcutils.getConnection();

            String sql = "update food set food_name = ?, food_type = ?,price = ?, is_halal =?,food_img = ?where id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,food.getFood_name());
            preparedStatement.setObject(2,food.getFood_type());
            preparedStatement.setObject(3,food.getPrice());
            preparedStatement.setObject(4,food.getIs_halal());
            preparedStatement.setObject(5,food.getFood_img());
            preparedStatement.setObject(6,food.getId());
            int count = preparedStatement.executeUpdate();
        if(count>0){
            flag = true;

        }
        return flag;

    }

    @Override
    public boolean delFoodbID(Food food) throws SQLException {
        boolean flag = false;
        Connection connection = jdbcutils.getConnection();


        String sql = "delete from food where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1, food.getId());
        int count = preparedStatement.executeUpdate();
        if (count > 0) {
            flag = true;


        } else {
            System.out.println("菜品不存在");
        }


        return flag;
    }

    public void addFood() throws SQLException {
        Food food = new Food();
        System.out.println("输入菜品名称");
        Scanner sc =new Scanner(System.in);
        food.setFood_name(sc.next());
        System.out.println("输入菜品类型");
        food.setFood_type(sc.next());
        System.out.println("输入菜品价格");
        food.setPrice(sc.nextInt());
        System.out.println("熟人菜品是否为清真，0代表是，1代表否");
        food.setIs_halal(sc.nextInt());
        System.out.println("输入图片来源");
        food.setFood_img(sc.next());
        //Fooddao fooddao1 = new Fooddaoimpl();
        //boolean bl= fooddao1.saveFood(food);
        boolean bl;
         bl= saveFood(food);
        if(bl){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    public void  upFood() throws SQLException {
        Food food2 = new Food();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入菜品id");


        food2.setId(sc.nextInt());
        System.out.println("输入菜品名称");
        food2.setFood_name(sc.next());
        System.out.println("输入菜品类型");
        food2.setFood_type(sc.next());
        System.out.println("输入菜品价格");
        food2.setPrice(sc.nextInt());
        System.out.println("熟人菜品是否为清真，0代表是，1代表否");
        food2.setIs_halal(sc.nextInt());
        System.out.println("输入图片来源");
        food2.setFood_img(sc.next());
        boolean  bl = upDateFood(food2);
        if(bl){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }


}
